import { PositionHelper } from "utils/position/PositionHelper";

 

/**
 * 房间位置拓展
 */
export default class PositionExtension extends RoomPosition 
{
    /**
     * 本房间的坐标距离
     * @param x 
     * @param y 
     * @returns 
     */
    public distance2(x:number,y:number):number
    {
        return PositionHelper.getDistance(this,{x:x,y:y});
    }
    public distance(p:IRoomPosition):number
    {
        return PositionHelper.Distance(this,p)
    }
    public directionToPos(direction: DirectionConstant): RoomPosition | undefined {
        let targetX = this.x
        let targetY = this.y

        // 纵轴移动，方向朝下就 y ++，否则就 y --
        if (direction !== LEFT && direction !== RIGHT) {
            if (direction > LEFT || direction < RIGHT) targetY --
            else targetY ++
        }
        // 横轴移动，方向朝右就 x ++，否则就 x --
        if (direction !== TOP && direction !== BOTTOM) {
            if (direction < BOTTOM) targetX ++
            else targetX --
        }

        // 如果要移动到另一个房间的话就返回空，否则返回目标 pos
        if (targetX < 0 || targetY > 49 || targetX > 49 || targetY < 0) return undefined
        else return PositionHelper.Create({x:targetX, y:targetY,roomName: this.roomName})
    }
    public getFreeSpace(range?:number,includeself?:boolean,excludestructure?:boolean): RoomPosition[]
    {
        return PositionHelper.getFreeSpace(this,range,includeself,excludestructure)
    }

    public lookForInRange<T extends keyof AllLookAtTypes>(type:T, range:number )
    {
        const min_x = this.x;
        const min_y = this.y;
        const max_x = this.x;
        const max_y = this.y;
        const top = Math.max(0,min_y-range);
        const left = Math.max(0,min_x-range);
        const right = Math.min(49,max_x+range);
        const bottom = Math.min(49,max_y+range);

        const ret = Game.rooms[this.roomName].lookForAtArea(type,top,left,bottom,right,true);
        return ret
    }
    to1D():number
    {
        return this.x+this.y*50;
    }
    
    lookMyCreep():Creep|undefined
    {
        if(!Game.rooms[this.roomName])
        {
            global.Log.Warning(`${this.roomName} 无视野`)
            return undefined
        }
        return Game.rooms[this.roomName].lookForMyCreep(this)
    }


    
    lookCreep():Creep|undefined
    {
        if(!Game.rooms[this.roomName])
        {
            global.Log.Warning(`${this.roomName} 无视野`)
            return undefined
        }
        return Game.rooms[this.roomName].lookForCreep(this)
    }
    // lookFor<T extends keyof AllLookAtTypes>(type: T): Array<AllLookAtTypes[T]>
    // {
    //     if(Game.rooms[this.roomName])
    //     {
    //         return Game.rooms[this.roomName].lookForAt(type,this);
    //     }
    //     return []
    // }
}

